home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 July / macformat-026.iso / mac / Shareware City / Science / µSim 1.0 folder / source / Dump.c < prev    next >
Encoding:
Text File  |  1995-04-07  |  11.2 KB  |  444 lines  |  [TEXT/MMCC]

  1. /*
  2. Copyright © 1993,1994,1995 by Fabrizio Oddone
  3. ••• ••• ••• ••• ••• ••• ••• ••• ••• •••
  4. This source code is distributed as freeware:
  5. you may copy, exchange, modify this code.
  6. You may include this code in any kind of application: freeware,
  7. shareware, or commercial, provided that full credits are given.
  8. You may not sell or distribute this code for profit.
  9. */
  10.  
  11. //#pragma load "MacDump"
  12.  
  13. #include    "UtilsSys7.h"
  14. #include    "CursorBalloon.h"
  15. #include    "Disasm.h"
  16. #include    "Dump.h"
  17. #include    "DoEditDialog.h"
  18. #include    "DoMenu.h"
  19. #include    "Globals.h"
  20. #include    "Main.h"
  21. #include    "Input.h"
  22. #include    "Registers.h"
  23. #include    "Scroll.h"
  24. #include    "TrackThumb.h"
  25. #include    "Conversions.h"
  26.  
  27. #if defined(FabSystem7orlater)
  28.  
  29. //#pragma segment Main
  30.  
  31. ControlHandle    dumpVScroll;
  32. short    dumpLineHeight, dumpFromTop, dumpCWidMax;
  33. static WindowPtr    curPosW;
  34.  
  35. static void NoRealTimeActionProc(ControlHandle control, short value);
  36. static void RealTimeActionProc(ControlHandle control, short value);
  37. static pascal void DumpActionProc(ControlHandle control, short part);
  38. static void DrawLine(Ptr start);
  39. static short TrackDumpObject(RectPtr r, short theLoc);
  40.  
  41. void DumpHome(void)
  42. {
  43. SetControlValue(dumpVScroll, GetControlMinimum(dumpVScroll));
  44. InvalDump();
  45. }
  46.  
  47. void DumpEnd(void)
  48. {
  49. SetControlValue(dumpVScroll, GetControlMaximum(dumpVScroll));
  50. InvalDump();
  51. }
  52.  
  53. void DumpPgUp(void)
  54. {
  55. DumpActionProc(dumpVScroll, kInPageUpControlPart);
  56. }
  57.  
  58. void DumpPgDn(void)
  59. {
  60. DumpActionProc(dumpVScroll, kInPageDownControlPart);
  61. }
  62.  
  63. void Activate_Dump(EventRecord *evt, WindowPtr w, Boolean becomingActive)
  64. {
  65. Rect    growRect;
  66.  
  67. /* the growbox needs to be redrawn on activation: */
  68. growRect = w->portRect;
  69. /* adjust for the scrollbars */
  70. growRect.top = growRect.bottom - kScrollbarAdjust + 1;
  71. growRect.left = growRect.right - kScrollbarAdjust + 1;
  72. if (becomingActive) {
  73.     InvalRect(&growRect);    /* we cannot avoid grow box flicker */
  74.     if ((*dumpVScroll)->contrlVis == 0) {
  75.         ShowControl(dumpVScroll);
  76.         ValidRect(&(*dumpVScroll)->contrlRect);
  77.         }
  78.     }
  79. else {
  80. /* the control must be redrawn on deactivation: */
  81.     HideControl(dumpVScroll);
  82.     InvalRect(&growRect);
  83.     }
  84. } /*Activate*/
  85.  
  86. void Grow_Dump(WindowPtr w, EventRecord *event)
  87. {
  88. Rect    tempRect, updateRect;
  89. register long    growResult;
  90.  
  91. tempRect.right = tempRect.left = PRCT_R(w) + 1;
  92. tempRect.bottom = 0x7FFF;        /* set up limiting values */
  93. tempRect.top = kMinDocDim + 10;
  94. updateRect = w->portRect;
  95. updateRect.top = updateRect.bottom - kScrollbarAdjust;
  96. /* see if it really changed size */
  97. if (growResult = GrowWindow(w, event->where, &tempRect)) {
  98.     SizeWindow(w, LoWrd(growResult),
  99.                 (HiWrd(growResult) / dumpLineHeight) * dumpLineHeight, true);
  100.     SizeControl(dumpVScroll, kScrollbarWidth, PRCT_B(w) - PRCT_T(w) - 13);
  101.     if (HiWrd(growResult) > updateRect.bottom)
  102.         /* enlarged */
  103.         InvalRect(&updateRect);
  104.     else /* reduced */ {
  105.         updateRect.bottom = PRCT_B(w);
  106.         updateRect.right = PRCT_R(w);
  107.         updateRect.top = updateRect.bottom - kScrollbarAdjust + 1;
  108.         updateRect.left = updateRect.right - kScrollbarAdjust + 1;
  109.         InvalRect(&updateRect);
  110.         }
  111.     ValidRect(&(*dumpVScroll)->contrlRect);
  112.     SetupDumpCtlMax(dumpVScroll);
  113.     }
  114. }
  115.  
  116. void Update_Dump(WindowPtr w)
  117. {
  118. Rect    growRect;
  119. register RgnHandle    oldClip;
  120.  
  121. if (EmptyRgn(w->visRgn) == false) {    /* draw if updating needs to be done */
  122.     DrawDump(w);
  123.     oldClip = NewRgn();
  124.     GetClip(oldClip);
  125.     growRect = w->portRect;
  126.     growRect.left = growRect.right - kScrollbarAdjust;
  127.     ClipRect(&growRect);
  128.     DrawGrowIcon(w);
  129.     SetClip(oldClip);
  130.     DisposeRgn(oldClip);
  131.     UpdateControls(w, w->visRgn);
  132.     }
  133. }
  134.  
  135. void Do_Dump(WindowPtr w, EventRecord *event)
  136. {
  137. enum {
  138. kCurPosWindow = 128
  139. };
  140.  
  141. Rect    tempRect;
  142. GrafPtr    savePort;
  143. ControlHandle    control;
  144. Point    mouse;
  145. register unsigned long    clickAddr;
  146. register short    part, offset, popItem;
  147.  
  148. mouse = event->where;        /* get the click position */
  149. GlobalToLocal(&mouse);
  150. /* see if we are in the dump area; if so, we won’t check the controls */
  151. tempRect = w->portRect;
  152. tempRect.right -= kScrollbarAdjust;
  153. if (PtInRect(mouse, &tempRect)) {
  154.     /* handle editing click */
  155.     if ((offset = mouse.h - dumpCWidMax * 6 - kDIST_FROMLEFT) >= 0)
  156.         if ((offset /= dumpCWidMax) % 5 != 4) {
  157.             tempRect.top = (mouse.v /= dumpLineHeight) * dumpLineHeight;
  158.             tempRect.left = ((offset / 5) * 5) * dumpCWidMax + dumpCWidMax * 6 + kDIST_FROMLEFT;
  159.             tempRect.bottom = tempRect.top + dumpLineHeight;
  160.             tempRect.right = tempRect.left + (dumpCWidMax << 2);
  161.             clickAddr = ((unsigned long)(GetControlValue(dumpVScroll) + mouse.v) << 4)
  162.                         + ((offset / 5) << 1);
  163.             if (popItem = TrackDumpObject(&tempRect, *(unsigned short *)(gMMemory + clickAddr))) {
  164.                 if (popItem == kD_Disasm) {
  165.                     SetControlValue(disasmVScroll, clickAddr >> 2);
  166.                     InvalDisasm();
  167.                     DoMenuWindows(kMItem_Disasm);
  168.                     }
  169.                 else if ((event->modifiers & optionKey)||(popItem == kD_DisasmFrom)) {
  170.                     SetControlValue(disasmVScroll, *(unsigned short *)(gMMemory + clickAddr) >> 1);
  171.                     InvalDisasm();
  172.                     DoMenuWindows(kMItem_Disasm);
  173.                     }
  174.                 else if ((event->modifiers & cmdKey)||(popItem == kD_DumpFrom)) {
  175.                     SetControlValue(dumpVScroll, *(unsigned short *)(gMMemory + clickAddr) >> 3);
  176.                     InvalDump();
  177.                     }
  178.                 else {
  179.                     if (DoEditDump((short *)(gMMemory + clickAddr), clickAddr >> 1)) {
  180.                         InvalDump();
  181.                         InvalDisasm();
  182.                         }
  183.                     UnloadSeg(DoEditDump);
  184.                     }
  185.                 }
  186.             }
  187.     }
  188. else {
  189.     part = FindControl(mouse, w, &control);
  190.     switch ( part ) {
  191.         case 0:        /* do nothing for viewRect case */
  192.             break;
  193.         case kInIndicatorControlPart:
  194.             if (gPrefs.NeXTScroll)
  195.                 (void)TrackThumb(control, mouse, RealTimeActionProc);
  196.             else {
  197.                 GetPort(&savePort);
  198.                 SetPort(curPosW = GetNewWindow(kCurPosWindow, nil, (WindowPtr)-1L));
  199.                 TextMode(srcCopy);
  200.                 TextFont(monaco);
  201.                 TextSize(9);
  202.                 SetPort(savePort);
  203.                 savePort = (GrafPtr)LMGetGhostWindow();
  204.                 LMSetGhostWindow(curPosW);
  205.                 ShowWindow(curPosW);
  206.                 (void)TrackThumb(control, mouse, NoRealTimeActionProc);
  207.                 DisposeWindow(curPosW);
  208.                 LMSetGhostWindow(savePort);
  209.                 InvalDump();
  210.                 }
  211.             break;
  212.         default:    /* clicked in an arrow, so track & scroll */
  213.             {
  214.             ControlActionUPP DumpActionProcUPP = NewControlActionProc(DumpActionProc);
  215.  
  216.             (void)TrackControl(control, mouse, DumpActionProcUPP);
  217.             if (DumpActionProcUPP)
  218.                 DisposeRoutineDescriptor(DumpActionProcUPP);
  219.             }
  220.             break;
  221.         }
  222.     }
  223. }
  224.  
  225. /* NoRealTimeActionProc: only updates the number in the little window up there */
  226.  
  227. static void NoRealTimeActionProc(ControlHandle , short value)
  228. {
  229. Str15    tempS;
  230. GrafPtr    savePort;
  231.  
  232. ShortToHexString(value << 3, tempS);
  233. GetPort(&savePort);
  234. SetPort(curPosW);
  235. MoveTo(kDIST_FROMLEFT,12);
  236. DrawString(tempS);
  237. SetPort(savePort);
  238. }
  239.  
  240. /* RealTimeActionProc: updates all of the window contents while dragging the scroll box */
  241.  
  242. static void RealTimeActionProc(ControlHandle control, short )
  243. {
  244. DrawDump((*control)->contrlOwner);
  245. }
  246.  
  247. static pascal void DumpActionProc(ControlHandle control, short part)
  248. {
  249. Rect    tempRect;
  250. register WindowPtr    w;
  251. register Ptr    addr;
  252. register short    amount, oldAmount, newAmount, vc;
  253. register Boolean    doScrollRect = false;
  254.  
  255. if ( part ) {        /* if it was actually in the control */
  256.     w = (*control)->contrlOwner;
  257.     switch ( part ) {
  258.         case kInUpButtonControlPart:
  259.             amount = -1;
  260.             doScrollRect = true;
  261.             break;
  262.         case kInDownButtonControlPart:
  263.             amount = 1;
  264.             doScrollRect = true;
  265.             break;
  266.         case kInPageUpControlPart:
  267.             amount = (PRCT_T(w) - PRCT_B(w)) / dumpLineHeight + 1;
  268.             break;
  269.         case kInPageDownControlPart:
  270.             amount = (PRCT_B(w) - PRCT_T(w)) / dumpLineHeight - 1;
  271.             break;
  272.         }
  273.     SetControlValue(control, (oldAmount = GetControlValue(control)) + amount);
  274.     if (doScrollRect && (newAmount = oldAmount - GetControlValue(control))) {
  275.         tempRect = w->portRect;
  276.         tempRect.right -= kScrollbarAdjust;
  277.         VScrollRect(&tempRect, newAmount * dumpLineHeight);
  278.         addr = &gMMemory[(unsigned long)GetControlValue(control) << 4];
  279.         vc = dumpFromTop;
  280.         if (newAmount < 0) {
  281.             vc += PRCT_B(w) - PRCT_T(w) - dumpLineHeight;
  282.             addr += ((PRCT_B(w) - PRCT_T(w)) / dumpLineHeight - 1) << 4;
  283.             }
  284.         MoveTo(PRCT_L(w) + kDIST_FROMLEFT, vc);
  285.         DrawLine(addr);
  286.         }
  287.     else
  288.         DrawDump(w);
  289.     }
  290. } /* DumpActionProc */
  291.  
  292. void DrawDump(WindowPtr w)
  293. {
  294. Rect    tempRect;
  295. GrafPtr    savePort;
  296. Point    tempPoint;
  297. register Ptr    addr;
  298. register short    j;
  299.  
  300. GetPort(&savePort);
  301. SetPort(w);
  302. tempRect = w->portRect;
  303. tempRect.bottom += dumpLineHeight;
  304. addr = &gMMemory[(unsigned long)GetControlValue(dumpVScroll) << 4];
  305. for(tempPoint.h = kDIST_FROMLEFT, j = dumpFromTop;
  306.     tempPoint.v = j, PtInRect(tempPoint, &tempRect);
  307.     j += dumpLineHeight, addr += 16) {
  308.  
  309.     MoveTo(PRCT_L(w) + kDIST_FROMLEFT, j);
  310.     DrawLine(addr);
  311.     }
  312. SetPort(savePort);
  313. }
  314.  
  315. /* DrawLine: draws a "line" of memory in the Dump window */
  316.  
  317. static void DrawLine(Ptr start)
  318. {
  319. Str63    tempS;
  320. register short *hexPtr, *endPtr;
  321. register Ptr    textPtr;
  322.  
  323. endPtr = (short *)(start + 16);
  324. textPtr = ShortToHexText(PTR2MEMWORD(start), (Ptr)&tempS);
  325. *textPtr++ = ':';
  326. for (hexPtr = (short *)start; hexPtr < endPtr; ) {
  327.     *textPtr++ = ' ';
  328.     textPtr = ShortToHexText(*hexPtr++, textPtr);
  329.     }
  330. DrawText(&tempS, 0, 45);
  331. }
  332.  
  333. /* SetupDumpCtlMax: sets up the CtlMax value of our scroll bar */
  334.  
  335. void SetupDumpCtlMax(ControlHandle theControl)
  336. {
  337. enum {
  338. kAdjustForPleasantGrow = 3
  339. };
  340.  
  341. register WindowPtr    wind;
  342. register short    newmax;
  343.  
  344. wind = (*theControl)->contrlOwner;
  345. newmax = 8191 - (PRCT_B(wind) - PRCT_T(wind) - kAdjustForPleasantGrow) / dumpLineHeight;
  346. if (newmax != GetControlMaximum(theControl)) {
  347.     SetControlMaximum(theControl, newmax);
  348.     InvalDump();
  349.     }
  350. }
  351.  
  352. void InvalDump(void)
  353. {
  354. Rect    tempRect;
  355. GrafPtr    savePort;
  356.  
  357. GetPort(&savePort);
  358. SetPort(gWPtr_Dump);
  359. tempRect = gWPtr_Dump->portRect;
  360. tempRect.right -= kScrollbarWidth;
  361. InvalRect(&tempRect);
  362. SetPort(savePort);
  363. }
  364.  
  365. /* TrackDumpObject: like all the TrackThing in the Mac OS, plus the popUp */
  366.  
  367. short    TrackDumpObject(RectPtr    r, short theLocation)
  368. {
  369. enum {
  370. kSimpleClick = -1,
  371. kDelay = 1 * 60 // one second
  372. };
  373.  
  374. Str32    itemStr;
  375. Str15    numStr;
  376. Point    myPt;
  377. MenuRef    tempMenu;
  378. register long        timeout = TickCount();
  379. register long        chosen = kSimpleClick;
  380. register Boolean    inrect;
  381.  
  382. InvertRect(r);
  383. inrect = true;
  384. do {
  385.     register Boolean tempB;
  386.  
  387.     GetMouse(&myPt);
  388.     if ((tempB = PtInRect(myPt, r)) != inrect) {
  389.         InvertRect(r);
  390.         inrect = tempB;
  391.         }
  392.     if (inrect == false)
  393.         timeout = TickCount();
  394.     if ((TickCount() - timeout) > GetDblTime()) {
  395.         tempMenu = gPopMenu;
  396.         if (noErr == HandToHand((Handle *)&tempMenu)) {
  397.             GetMenuItemText(tempMenu, kD_DisasmFrom, itemStr);
  398.             ShortToHexString(theLocation, numStr);
  399.             PLstrcat(itemStr, numStr);
  400.             SetMenuItemText(tempMenu, kD_DisasmFrom, itemStr);
  401.             GetMenuItemText(tempMenu, kD_DumpFrom, itemStr);
  402.             PLstrcat(itemStr, numStr);
  403.             SetMenuItemText(tempMenu, kD_DumpFrom, itemStr);
  404.             InsertMenu(tempMenu, hierMenu);
  405.             LocalToGlobal(&myPt);
  406.             chosen = PopUpMenuSelect(tempMenu, myPt.v, myPt.h, kD_Edit);
  407.             DeleteMenu(kRes_Menu_PopDump);
  408.             DisposeMenu(tempMenu);
  409.             }
  410.         }
  411.     }
  412. while( StillDown() );
  413. if (inrect)
  414.     InvertRect(r);
  415. if (chosen == kSimpleClick)
  416.     chosen = inrect ? toMenu(kRes_Menu_PopDump, kD_Edit) : 0L;
  417. return HiWrd(chosen) ? LoWrd(chosen) : 0;
  418. }
  419.  
  420. /* procedure called when closing the Dump window */
  421.  
  422. void CloseDump(WindowPtr w)
  423. {
  424. DoCloseWindow(w, kMItem_Dump);
  425. }
  426.  
  427. void RecalcDump(FabWindowPtr w, RgnBalloonCursPtr theObj)
  428. {
  429. Rect    tempRect;
  430.  
  431. tempRect = ((WindowPtr)w)->portRect;
  432. tempRect.right -= kScrollbarWidth;
  433. RectRgn(theObj->zoneLocal, &tempRect);
  434. }
  435.  
  436. void getDragRectDump(WindowPtr w, RectPtr r)
  437. {
  438. *r = w->portRect;
  439. r->right -= kScrollbarAdjust;
  440. }
  441.  
  442. #endif
  443.  
  444.